package com.maxjay.component.simplelist.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.maxjay.component.metadata.model.Record;
import com.maxjay.component.metadata.support.EntityRecordProcessor;
import com.maxjay.component.simplelist.model.QueryScheme;
import com.maxjay.component.simplelist.service.PaginationService;
import com.maxjay.component.simplelist.service.QuerySchemeService;
import com.maxjay.component.simplelist.service.QuerySqlProcessor;
import com.maxjay.system.service.BaseServiceJdbc;

@Service("querySchemeService")
public class QuerySchemeServiceImpl extends BaseServiceJdbc implements QuerySchemeService {

	@Resource(name = "paginationServiceJdbcMysql")
	private PaginationService paginationService;
	
	@Resource
	private QuerySqlProcessor querySqlProcessor;

	@Override
	public List<Record> getRecordList(QueryScheme queryScheme, Map<String, Object> queryParams) {
		List<Map<String, Object>> dataMapList = this.getRecordMapList(queryScheme, queryParams, 0, -1);
		List<Record> records = new ArrayList<Record>();
		for (Map<String, Object> dataMap : dataMapList) {
			records.add(new Record(dataMap));
		}
		return records;
	}

	@Override
	public List<Record> getRecordList(QueryScheme queryScheme, Map<String, Object> queryParams, int startRow, int endRow) {
		List<Map<String, Object>> dataMapList = this.getRecordMapList(queryScheme, queryParams, startRow, endRow);
		List<Record> records = new ArrayList<Record>();
		for (Map<String, Object> dataMap : dataMapList) {
			records.add(new Record(dataMap));
		}
		return records;
	}
	
	@Override
	public List<Record> getDisplayRecordList(QueryScheme queryScheme, Map<String, Object> queryParams) {
		List<Map<String, Object>> dataMapList = this.getRecordMapList(queryScheme, queryParams, 0, -1);
		return generateDisplayRecords(queryScheme, dataMapList);
	}

	@Override
	public List<Record> getDisplayRecordList(QueryScheme queryScheme, Map<String, Object> queryParams, int startRow, int endRow) {
		List<Map<String, Object>> dataMapList = this.getRecordMapList(queryScheme, queryParams, startRow, endRow);
		return generateDisplayRecords(queryScheme, dataMapList);
	}
	
	@Override
	public List<Map<String, Object>> getRecordMapList(QueryScheme queryScheme, Map<String, Object> queryParams, int startRow, int endRow) {
		String querySql = querySqlProcessor.getSchemeQuerySql(queryScheme, queryParams);
		List<Map<String, Object>> dataMapList = paginationService.getPaginationData(querySql, queryParams, startRow, endRow);
		return dataMapList;
	}

	@Override
	public int getRecordCount(QueryScheme queryScheme, Map<String, Object> queryParams) {
		String querySql = querySqlProcessor.getSchemeQuerySql(queryScheme, queryParams);
		return paginationService.getDataCount(querySql, queryParams);
	}
	
	private List<Record> generateDisplayRecords(QueryScheme queryScheme, List<Map<String, Object>> dataMapList) {
		List<Record> records = new ArrayList<Record>();
		for(Map<String, Object> dataMap : dataMapList) {
			Record storeRecord = new Record(dataMap);
			Record displayRecord = EntityRecordProcessor.convertDisplayRecord(queryScheme.getEntity(), storeRecord);
			records.add(displayRecord);
		}
		return records;
	}
	
}
